package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.util.PreambleoptElementUtil;

/**
 * The complex type preambleopt defines the content model and attributes used by preambles. Here the id attribute is
 * optional.
 * 
 * <pre>
 * 	<xsd:complexType name="preambleopt">
 * 		<xsd:choice minOccurs="1" maxOccurs="unbounded">
 * 			<xsd:group ref="blockElements"/>
 * 			<xsd:group ref="preambleContainers"/>
 * 		</xsd:choice>
 * 		<xsd:attributeGroup ref="coreopt"/>
 * </xsd:complexType>
 * 
 * <pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public abstract class Preambleopt extends CoreOptImpl {

	private final List<PreambleoptElement> _elements = new ArrayList<PreambleoptElement>(4);

	public List<PreambleoptElement> getElements() {
		return _elements;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		super.writeExternal(out);
		for (int i = 0, n = _elements.size(); i < n; i++) {
			_elements.get(i).writeExternal(out);
		}
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		super.readExternal(in);
		final String localName = in.getLocalName();
		in.readNextStartElement();
		PreambleoptElementUtil.read(in, _elements, localName);
	}

}